#!/usr/bin/perl

# Encode/decode wierd encoding found in LWI (McAfee TC 6.0) scripts.

open DATA,$file=$ARGV[0]||'451StaAP.lws';
binmode DATA;
open STDOUT,'>'.$' if $file=~/^451/;
binmode STDOUT;
{undef local $/;$_=<DATA>;close DATA;}

s/\[ScrStart\](.*)\[ScrEnd\]/"[tratSrcS]".dec(1,$1)."[dnEcrS]"/se unless s/\[tratSrcS\](.*)\[dnEcrS\]/"[ScrStart]".dec(0,$1)."[ScrEnd]"/se;
print ;#dec($1);

print STDERR "{$action}\n";

sub dec($$)
{
$action=$_[0]?"Protected":"Decrypted";
my @buf=split//,$_[1];my $len=@buf;$len-- if $len&1;
scr($_[0],$len,\@buf);scr($_[0],$len,\@buf);scr($_[0],$len,\@buf);
return join'',@buf;
}

sub scr($$@)
{
	my @src=@{$_[2]};
	my $i=0;
	my $beg=0;
	my $end=$_[1]-1;

	while ($i<$_[1])
	{
		if ($_[0]) {
			${$_[2]}[$i++]=$src[$beg++];
			${$_[2]}[$i++]=$src[$end--];
		} else {
			${$_[2]}[$beg++]=$src[$i++];
			${$_[2]}[$end--]=$src[$i++];
		}
	}
}
